#ifndef __BSP_MC_DRV8323_H
#define __BSP_MC_DRV8323_H

#ifdef __cplusplus
extern "C" {
#endif
	
#include "bsp_mc_type.h"
#include "bsp_mc_globals.h"
#include "delay.h"
#include "bsp_mc_svpwm_3shunt.h"

	
#define MOTOR_SPI                         SPI3
#define MOTOR_SPI_CLK                     RCC_APB1Periph_SPI3  
	
#define MOTOR_SPI_CS_CLK                  RCC_AHB1Periph_GPIOA  
#define MOTOR_SPI_CS_GPIO_PORT            GPIOA   
#define MOTOR_SPI_CS_PIN                  GPIO_Pin_15

	
#define MOTOR_SPI_GPIO_CLK                RCC_AHB1Periph_GPIOC  
#define MOTOR_SPI_GPIO_PORT               GPIOC   
	
#define MOTOR_SPI_CLK_PIN                 GPIO_Pin_10
#define MOTOR_SPI_MISO_PIN                GPIO_Pin_11   
#define MOTOR_SPI_MOSI_PIN                GPIO_Pin_12 
	
#define	MOTOR_SPI_CLK_PINSource           GPIO_PinSource10
#define	MOTOR_SPI_MISO_PINSource          GPIO_PinSource11
#define	MOTOR_SPI_MOSI_PINSource          GPIO_PinSource12
	
	
//POWER ON
#define DRV8323_POWER_PIN              GPIO_Pin_0
#define DRV8323_POWER_GPIO_PORT        GPIOE                     
#define DRV8323_POWER_GPIO_CLK    	   RCC_AHB1Periph_GPIOE

//CAL
#define DRV8323_CAL_PIN              GPIO_Pin_2
#define DRV8323_CAL_GPIO_PORT        GPIOE                     
#define DRV8323_CAL_GPIO_CLK    	   RCC_AHB1Periph_GPIOE

	
//Motor ENABLE to drv8323
#define DRV8323_ENABLE_PIN           GPIO_Pin_11
#define DRV8323_ENABLE_GPIO_PORT     GPIOB                     
#define DRV8323_ENABLE_GPIO_CLK      RCC_AHB1Periph_GPIOB

//Motor fault
#define DRV8323_FAULT_PIN           GPIO_Pin_3
#define DRV8323_FAULT_GPIO_PORT     GPIOE                     
#define DRV8323_FAULT_GPIO_CLK      RCC_AHB1Periph_GPIOE

//Brake from MCU
#define Motor_BRAKE_PIN              GPIO_Pin_1
#define Motor_BRAKE_GPIO_PORT        GPIOE                     
#define Motor_BRAKE_GPIO_CLK         RCC_AHB1Periph_GPIOE

//Test Pin
#define Test_PIN             		 GPIO_Pin_15
#define Test_GPIO_PORT        		 GPIOE                     
#define Test_GPIO_CLK         	     RCC_AHB1Periph_GPIOE

#define TEST_PIN_ON    				 {GPIO_SetBits(Test_GPIO_PORT,Test_PIN);}
#define TEST_PIN_OFF    			 {GPIO_ResetBits(Test_GPIO_PORT,Test_PIN);}
#define TEST_PIN_TOGGLE         	 {GPIO_ToggleBits(Test_GPIO_PORT,Test_PIN);}


// Drv8323 registers
#define DRV8323_R      (0x01<<(15))
#define DRV8323_W      (0x00<<(15))
	
#define DRV8323_ADDRESS0      (0x00<<(11))
#define DRV8323_ADDRESS1      (0x01<<(11))
#define DRV8323_ADDRESS2      (0x02<<(11))
#define DRV8323_ADDRESS3      (0x03<<(11))
#define DRV8323_ADDRESS4      (0x04<<(11))
#define DRV8323_ADDRESS5      (0x05<<(11))
#define DRV8323_ADDRESS6      (0x06<<(11))
#define DRV8323_ADDRESS7      (0x07<<(11))


/*Driver Control Register (address = 0x02)*/
#define DIS_CPUV     (0x00<<(9))
#define DIS_GDF      (0x00<<(8))
#define OTW_REP      (0x01<<(7))
#define PWM_MODE     (0x00<<(5))
#define PWM1_COM     (0x00<<(4))
#define PWM1_DIR     (0x00<<(3))
#define COAST        (0x00<<(2))
#define BRAKE        (0x01<<(1))
#define NOBRAKE      (0x00<<(1))
#define CLR_FLT      (0x00<<(0))

/*Gate Drive HS Register (address = 0x03)*/
#define UNLOCK          (0x03<<(8))
#define LOCK            (0x06<<(8))
#define IDRIVEP_HS      (0x0B<<(4))
#define IDRIVEN_HS      (0x0B<<(0))


/*Gate Drive LS Register (address = 0x04)*/
#define CBC          (0x01<<(10))
#define TDRIVE       (0x02<<(8))//2us gate  peak current 
#define IDRIVEP_LS   (0x09<<(4))
#define IDRIVEN_LS   (0x09<<(0))


/*OCP Control Register (address = 0x05)*/
#define TRETRY        (0x01<<(10))
#define DEAD_TIME     (0x00<<(8))//500ns
#define OCP_MODE      (0x01<<(6))
#define OCP_DEG       (0x03<<(4))
#define VDS_LVL       (0x09<<(0))

/*CSA Control Register (address = 0x06)*/
#define CSA_FET         (0x00<<(10))
#define VREF_DIV        (0x01<<(9))
#define LS_REF          (0x00<<(8))
#define CSA_GAIN        (0x01<<(6))
#define DIS_SEN         (0x00<<(5))
#define CSA_CAL_A       (0x00<<(4))
#define CSA_CAL_B       (0x00<<(3))
#define CSA_CAL_C       (0x00<<(2))
#define SEN_LVL         (0x03<<(0))




void SPI_GPIO_Init(void);	
void SPI_Mode_Init(void);
u16  Spi_Send_HalfWord(u16 sendata);
void SPI_Drv8323_Init(void);
void MotorDrv_SPI_Init(void);

void Motor_Brake(void);
void Motor_Start(void);
void Motor_Stop( void );
void Motor_Enable(void);
void Motor_Disable(void);



	
#ifdef __cplusplus
}
#endif

#endif






